﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sugar.Enties;
using HZH_Controls.Controls;
using Test.UC;
using System.Data.SqlClient;
using Seating.utils;
namespace Seating
{
    public partial class Reservation_Manage_From : Form
    {
        public bool IsStudent { get; set; }
        public Reservation_Manage_From()
        {
            InitializeComponent();
        }
        public Reservation_Manage_From(bool isStudent)
        {
            this.IsStudent = isStudent;
            InitializeComponent();
        }
        private void Student_Manage_From_Load(object sender, EventArgs e)
        {
            initGrid();

        }

        private void initGrid()
        {
            var db = new Tb_ReservationManager();
            List<Tb_Reservation> list = new List<Tb_Reservation>();
            if (this.IsStudent)
            {
                list = new List<Tb_Reservation> { };
            }
            else
            {
                list = db.GetList();

            }
            dataGridView_student.Columns.Clear();

            dataGridView_student.ReadOnly = true;
            dataGridView_student.DataSource = fillStateName(list);
            // 屏蔽state
            dataGridView_student.Columns[3].Visible = false;

                addBtns();
            
        }

        private List<Tb_Reservation> fillStateName(List<Tb_Reservation> list)
        {
            foreach ( Tb_Reservation item in list)
            {
               item.StateName= getStateName(item.State);
            }
            return list;
        }

        private string getStateName(byte? state)
        {
            switch (state) { 
                case 9:
                    return "违约";
                case 0:
                    return "取消";
                case 1:
                    return "预约";
                case 2:
                    return "履约";
                default:
                    return "未知";
            }
        }

        private void addBtns()
        {
            if (this.IsStudent)
            {

            }
            else
            {
                // 添加按钮
                DataGridViewButtonColumn btnFinish = new DataGridViewButtonColumn();
                btnFinish.Name = "btnFinish";
                btnFinish.HeaderText = "";
                btnFinish.DefaultCellStyle.NullValue = "履约";
                dataGridView_student.Columns.Add(btnFinish);
            }


            DataGridViewButtonColumn btnCancle = new DataGridViewButtonColumn();
            //Tb_Reservation item = (Tb_Reservation)dataGridView_student.CurrentRow.DataBoundItem;
            //btnCancle.Visible = item.State == 0;
            btnCancle.Name = "btnCancel";
            btnCancle.HeaderText = "";
            btnCancle.DefaultCellStyle.NullValue = "取消";
            dataGridView_student.Columns.Add(btnCancle);
        }

        private void dataGridView_student_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            //SqlDataAdapter dtAdapter = new SqlDataAdapter();
            //if (this.dataGridView_student.IsCurrentCellDirty)
            //{
            //    //this.dataGridView_student.CommitEdit(DataGridViewDataErrorContexts.Commit);

            //}

        }

        //行值变化

        private void dataGridView_student_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow dataGridViewRow = dataGridView_student.CurrentRow;
            Tb_Student student = dataGridViewRow.DataBoundItem as Tb_Student;
            Tb_StudentManager manager = new Tb_StudentManager();

            try
            {
                manager.Update(student);
            }
            catch (Exception)
            {
                MessageBox.Show("更新失败");
            }
        }

        private void dataGridView_student_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //点击button按钮事件
            if (e.RowIndex < 0)
            {
                return;
            }
            Tb_Reservation item = (Tb_Reservation)dataGridView_student.CurrentRow.DataBoundItem;

            switch (dataGridView_student.Columns[e.ColumnIndex].Name)
            {
                case "btnFinish":
                    item.State = 2;
                    new Tb_ReservationManager().Update(item);
                    MessageBox.Show("操作成功");
                    initGrid();
                    break;
                case "btnCancel":
                    item.State = 0;
                    item.CancelTime = DateTime.Now;
                    new Tb_ReservationManager().Update(item);
                    MessageBox.Show("操作成功");
                    initGrid();
                    break;

            }

        }

    }
}
